package com.ztc.AppUser.web;

import com.aqgj.User.bean.User;
import com.aqgj.util.DateFarmatUtils;
import com.aqgj.util.JsonWrapper;
import com.ztc.AppUser.bean.AppUser;
import com.ztc.AppUser.service.AppUserManager;
import com.ztc.ZtcCar.bean.ZtcCar;
import com.ztc.ZtcCar.service.ZtcCarManager;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpSession;
import java.util.Date;
import java.util.HashMap;
import java.util.List;


@Controller
@RequestMapping("/ztc/appUser")
public class AppUserAction{

    @Autowired
    private AppUserManager appUserManager;
    @Autowired
    private ZtcCarManager ztcCarManager;

    /**
     * 获取顺风车/快递用户列表.
     * @param pageNo    页 码
     * @param pageSize 每页数量
     * @param search    搜索条件
     * @param distinguishType  类别（1 除待审核数据，2待审核数据）
     * @param isExpress  类别（0 顺风车，1快递员）
     * @return
     */
    @RequestMapping("getPrivateCarUserList")
    @ResponseBody
    public HashMap<String,Object> getPrivateCarUserList(@RequestParam(defaultValue = "1") Integer pageNo, @RequestParam(defaultValue = "10")Integer pageSize, String search, @RequestParam(defaultValue = "1")Integer distinguishType, @RequestParam(defaultValue = "0") Integer isExpress, HttpSession session){
        User user=(User) session.getAttribute("userInfo");
       return appUserManager.getPrivateCarUserListByType(pageNo,pageSize,search,distinguishType,isExpress,user.getTransportId());
    }

    /**
     * 获取用户列表.
     * @param pageNo    页 码
     * @param pageSize 每页数量
     * @param search    搜索条件
     * @return
     */
    @RequestMapping("getAppUserList")
    @ResponseBody
    public HashMap<String,Object> getAppUserList(@RequestParam(defaultValue = "1") Integer pageNo,@RequestParam(defaultValue = "10")Integer pageSize,String search){
        return appUserManager.getAppUserList(pageNo,pageSize,search);
    }

    /**
     * 用户详情.
     * @param uid
     * @return
     */
    @RequestMapping("examineUserInfo")
    @ResponseBody
    public HashMap<String,Object> examineUserInfo(@RequestParam(value = "uid",required = true) String uid){
        if(uid==null||"".equals(uid.trim())){
            return JsonWrapper.failureWrapperMsg("获取用户信息失败！");
        }
        AppUser appUser= appUserManager.get(uid);
        if(appUser==null){
            return JsonWrapper.failureWrapperMsg("用户信息不存在！");
        }
      return JsonWrapper.successWrapper(appUserManager.getUserInfo(appUser));
    }

    /**
     * 审核顺风车主/快递员申请.
     * @param uid 用户ID
     * @param type 审核类型（1通过，2不通过）
     * @param isExpress 类型（0顺风车，1快递员）
     * @return
     */
    @RequestMapping("examineOperation")
    @ResponseBody
    public HashMap<String,Object> examineOperation(String uid,Integer type,String remark,String certificateEndTime,@RequestParam(defaultValue = "0") Integer isExpress){
        if(uid==null||"".equals(uid.trim())){
            return JsonWrapper.failureWrapperMsg("获取用户信息失败！");
        }
        if(type!=1&&type!=2){
            return JsonWrapper.failureWrapperMsg("参数错误！");
        }
        AppUser appUser= appUserManager.get(uid);
        if(appUser==null){
            return JsonWrapper.failureWrapperMsg("用户信息不存在！");
        }
        if(type==1){
            if (isExpress == 0) {
                List<ZtcCar> ztcCars = ztcCarManager.findBy("transportId", uid);
                if (ztcCars.size() <= 0) {
                    return JsonWrapper.failureWrapperMsg("车信息不存在！");
                }
                if(certificateEndTime==null||"".equals(certificateEndTime.trim())){
                    return JsonWrapper.failureWrapperMsg("证件到期日期未填写！");
                }
                appUser.setCertificateEndTime(DateFarmatUtils.stringToDate(certificateEndTime,DateFarmatUtils.FORMAT_SHORT));
                if(appUser.getType()==1){
                    appUser.setType(2);
                }else if(appUser.getType()==2){
                    appUser.setType(2);
                }else{
                    appUser.setType(4);
                }
                appUser.setStatus(1);
            }else{
                if(appUser.getType()==1){
                    appUser.setType(3);
                }else if(appUser.getType()==3){
                    appUser.setType(3);
                }else{
                    appUser.setType(4);
                }
                appUser.setExpStatus(3);
            }
        }else{
            if (isExpress == 0) {
                appUser.setStatus(4);
                appUser.setType(2);
            }else{
                appUser.setExpStatus(2);
                appUser.setType(3);
            }
        }
        appUser.setExamineRemark(remark);
        appUser.setExamineTime(new Date());
        appUserManager.save(appUser);
        return JsonWrapper.successWrapper("操作成功！");
    }

    /**
     * 冻结/启用.
     * @param uid
     * @return
     */
    @RequestMapping("frozenAppUserInfo")
    @ResponseBody
    public HashMap<String,Object> frozenAppUserInfo(@RequestParam(value = "uid",required = true) String uid,Integer status){
        if(uid==null||"".equals(uid.trim())){
            return JsonWrapper.failureWrapperMsg("获取用户信息失败！");
        }
        if(status!=2&&status!=1){
            return JsonWrapper.failureWrapperMsg("状态参数错误！");
        }
        AppUser appUser= appUserManager.get(uid);
        if(appUser==null){
            return JsonWrapper.failureWrapperMsg("用户信息不存在！");
        }
        appUser.setStatus(status);
        appUserManager.save(appUser);
        return JsonWrapper.successWrapper("操作成功！");
    }

    /**
     * 冻结/启用 快递员/顺风车角色.
     * @param uid  用户id
     * @param status  冻结状态（3启用,4冻结）
     * @param isExpress  是否快递员角色（1是,2否）
     * @return
     */
    @RequestMapping("frozenExpUserInfo")
    @ResponseBody
    public HashMap<String,Object> frozenExpUserInfo(@RequestParam(value = "uid",required = true) String uid,Integer status,@RequestParam(defaultValue = "1") Integer isExpress){
        if(uid==null||"".equals(uid.trim())){
            return JsonWrapper.failureWrapperMsg("获取用户信息失败！");
        }
        if(status!=3&&status!=4){
            return JsonWrapper.failureWrapperMsg("状态参数错误！");
        }
        AppUser appUser= appUserManager.get(uid);
        if(appUser==null){
            return JsonWrapper.failureWrapperMsg("用户信息不存在！");
        }
        if (isExpress==1){
            appUser.setExpStatus(status);
        }else{
            appUser.setPrivateCarStatus(status);
        }
        appUserManager.save(appUser);
        return JsonWrapper.successWrapper("操作成功！");
    }
}